//
//  AYStatusViewModel.swift
//  iOSSinaWeb
//
//  Created by yws on 16/5/17.
//  Copyright © 2016年 yws. All rights reserved.
//

import UIKit

class AYStatusViewModel: NSObject {

    
    /// 单例
    static let sharedInstance: AYStatusViewModel = AYStatusViewModel()
    
    /// 私有构造函数
    private override init() {
        super.init()
    }
    
    func loadStatus(since_id: Int64, max_id: Int64, loadStatusCallback: (statuses: [AYStatus]?, error: NSError?) -> Void) {
        
        guard let access_token = AYUserAccountViewModel.sharedInstance.userAccount?.access_token else {
            AMPrint(items: "access_token没值,不去请求微博数据")
            return
        }
        let urlString = "2/statuses/home_timeline.json"
        
        var parameters: [String: AnyObject] = [
            "access_token" : access_token
        ]
        
        if since_id > 0  {
            parameters["since_id"] = NSNumber(longLong: since_id)
        } else if max_id > 0 {
            parameters["max_id"] = NSNumber(longLong: max_id)
        }
        
        AYAFNetworkTool.sharedInstance.request(requestMethod.GET, URLString: urlString, parameters: parameters, success: { (_, responseObject) in
            //            AMPrint(items: "获取微博数据成功了")
            if let result = responseObject as? [String: AnyObject] {
                if let statuses = result["statuses"] as? [[String: AnyObject]] {
                    var statusArray = [AYStatus]()
                    for dict in statuses {
                        
                        let statusModel = AYStatus(dict: dict)
                        
                        statusArray.append(statusModel)
                    }
                    loadStatusCallback(statuses: statusArray, error: nil)
//                                        AMPrint(items: "微博数组\(statusArray)")
                }
            }
            }, failure: { (_, error) in
//                AMPrint(items: "获取微博数据失败了\(error)")
                loadStatusCallback(statuses: nil, error: error)
        })
        
    }
    
    
    
}
